import { IsString, IsOptional, IsBoolean, IsNumber, Min, Max, IsUrl } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';

export class ProxyServerConfigDto {
  @ApiPropertyOptional({ description: 'Enable proxy server', example: false })
  @IsOptional()
  @IsBoolean()
  enabled?: boolean;

  @ApiPropertyOptional({ description: 'Proxy server URL', example: 'http://proxy.example.com:8080' })
  @IsOptional()
  @IsString()
  @IsUrl({ require_protocol: true })
  url?: string;

  @ApiPropertyOptional({ description: 'Proxy authentication username' })
  @IsOptional()
  @IsString()
  username?: string;

  @ApiPropertyOptional({ description: 'Proxy authentication password' })
  @IsOptional()
  @IsString()
  password?: string;
}

export class CopilotConfigDto {
  @ApiProperty({ description: 'Copilot API URL', example: 'https://api.individual.githubcopilot.com' })
  @IsString()
  apiUrl: string;

  @ApiProperty({ description: 'Copilot auth token' })
  @IsString()
  authToken: string;

  @ApiProperty({ description: 'Default model', example: 'claude-3.5-sonnet' })
  @IsString()
  defaultModel: string;

  @ApiPropertyOptional({ description: 'Request timeout in ms', example: 30000 })
  @IsOptional()
  @IsNumber()
  @Min(1000)
  @Max(300000)
  timeout?: number;

  @ApiPropertyOptional({ description: 'Retry attempts', example: 3 })
  @IsOptional()
  @IsNumber()
  @Min(0)
  @Max(10)
  retryAttempts?: number;

  @ApiPropertyOptional({ description: 'User agent', example: 'GithubCopilot/1.348.0' })
  @IsOptional()
  @IsString()
  userAgent?: string;

  @ApiPropertyOptional({ description: 'Integration ID', example: 'jetbrains-chat' })
  @IsOptional()
  @IsString()
  integrationId?: string;

  @ApiPropertyOptional({ description: 'LSP version', example: '1.348.0' })
  @IsOptional()
  @IsString()
  lspVersion?: string;

  @ApiPropertyOptional({ description: 'Plugin version', example: 'copilot-intellij/1.5.52-241' })
  @IsOptional()
  @IsString()
  pluginVersion?: string;

  @ApiPropertyOptional({ description: 'Editor version', example: 'JetBrains-IU/241.14494.240' })
  @IsOptional()
  @IsString()
  editorVersion?: string;

  @ApiPropertyOptional({ description: 'Machine ID' })
  @IsOptional()
  @IsString()
  machineId?: string;

  @ApiPropertyOptional({ description: 'Session ID' })
  @IsOptional()
  @IsString()
  sessionId?: string;

  @ApiPropertyOptional({ type: ProxyServerConfigDto, description: 'Proxy server configuration' })
  @IsOptional()
  proxy?: ProxyServerConfigDto;
}

export class ProxyConfigDto {
  @ApiProperty({ 
    enum: ['mock', 'copilot'], 
    description: 'Proxy mode',
    example: 'mock'
  })
  @IsString()
  mode: 'mock' | 'copilot';

  @ApiPropertyOptional({ description: 'Enable debug mode' })
  @IsOptional()
  @IsBoolean()
  debug?: boolean;

  @ApiPropertyOptional({ description: 'Request timeout in ms' })
  @IsOptional()
  @IsNumber()
  @Min(1000)
  @Max(300000)
  timeout?: number;

  @ApiPropertyOptional({ description: 'Max retry attempts' })
  @IsOptional()
  @IsNumber()
  @Min(0)
  @Max(10)
  maxRetries?: number;

  @ApiPropertyOptional({ description: 'Log requests' })
  @IsOptional()
  @IsBoolean()
  logRequests?: boolean;

  @ApiPropertyOptional({ description: 'Log responses' })
  @IsOptional()
  @IsBoolean()
  logResponses?: boolean;

  @ApiPropertyOptional({ type: CopilotConfigDto, description: 'Copilot configuration' })
  @IsOptional()
  copilot?: CopilotConfigDto;
}

export class UpdateCopilotConfigDto {
  @ApiPropertyOptional({ description: 'GitHub token for authentication (ghu_...)' })
  @IsOptional()
  @IsString()
  githubToken?: string;

  @ApiPropertyOptional({ description: 'Copilot auth token (tid=...;exp=...)' })
  @IsOptional()
  @IsString()
  authToken?: string;

  @ApiPropertyOptional({ description: 'Copilot API URL', example: 'https://api.individual.githubcopilot.com' })
  @IsOptional()
  @IsString()
  @IsUrl({ require_protocol: true })
  apiUrl?: string;

  @ApiPropertyOptional({ description: 'Default model' })
  @IsOptional()
  @IsString()
  defaultModel?: string;

  @ApiPropertyOptional({ description: 'Request timeout in ms' })
  @IsOptional()
  @IsNumber()
  @Min(1000)
  @Max(300000)
  timeout?: number;

  @ApiPropertyOptional({ description: 'Retry attempts' })
  @IsOptional()
  @IsNumber()
  @Min(0)
  @Max(10)
  retryAttempts?: number;

  @ApiPropertyOptional({ type: ProxyServerConfigDto, description: 'Proxy server configuration' })
  @IsOptional()
  proxy?: ProxyServerConfigDto;
}

export class TestConnectionDto {
  @ApiPropertyOptional({ description: 'Test message', example: 'Hello, this is a test message' })
  @IsOptional()
  @IsString()
  testMessage?: string;
}

export class TestConnectionResponseDto {
  @ApiProperty({ description: 'Test result' })
  success: boolean;

  @ApiProperty({ description: 'Response message' })
  message: string;

  @ApiPropertyOptional({ description: 'Response time in ms' })
  responseTime?: number;

  @ApiPropertyOptional({ description: 'Error details' })
  error?: string;

  @ApiPropertyOptional({ description: 'Token info' })
  tokenInfo?: {
    valid: boolean;
    expiresAt?: string;
    sku?: string;
    userId?: string;
  };
}

export class ProxyConfigResponseDto {
  @ApiProperty({ description: 'Current proxy mode' })
  mode: string;

  @ApiProperty({ description: 'Debug mode enabled' })
  debug: boolean;

  @ApiProperty({ description: 'Request timeout' })
  timeout: number;

  @ApiProperty({ description: 'Max retries' })
  maxRetries: number;

  @ApiProperty({ description: 'Log requests enabled' })
  logRequests: boolean;

  @ApiProperty({ description: 'Log responses enabled' })
  logResponses: boolean;

  @ApiProperty({ description: 'Copilot configuration' })
  copilot: {
    apiUrl: string;
    defaultModel: string;
    hasGithubToken: boolean;
    hasAuthToken: boolean;
    tokenValid?: boolean;
    tokenExpiry?: string;
    userAgent: string;
    proxy?: {
      enabled: boolean;
      url?: string;
      hasAuth: boolean;
    };
  };
}

export class SwitchModeDto {
  @ApiProperty({ 
    enum: ['mock', 'copilot'], 
    description: 'Target proxy mode' 
  })
  @IsString()
  mode: 'mock' | 'copilot';
}